package sl;

public class Saolei {

	public static boolean execute() {
		int[] pid = new int[1];
		int hwnd = User32.INSTANCE.FindWindowA(null, "扫雷");
		if (hwnd == 0) {
			System.out.println("请先打开WINDOWS自带的扫雷游戏");
			return false;
		}
		System.out.println("窗口句柄 = " + hwnd);

		User32.INSTANCE.GetWindowThreadProcessId(hwnd, pid);
		System.out.println("进程标识 = " + pid[0]);

		int hProcess = Kernel32.INSTANCE.OpenProcess(16, false, pid[0]);
		System.out.println("进程句柄 = " + hProcess);

		byte[] data = new byte[1024];
		if (!Kernel32.INSTANCE.ReadProcessMemory(hProcess, 16798560, data,
				1024, 0)) {
			System.out.println("读取内存出错");
			return false;
		}

		byte[] xy = new byte[8];
		if (!Kernel32.INSTANCE.ReadProcessMemory(hProcess, 16798516, xy, 8, 0)) {
			System.out.println("读取内存出错");
			return false;
		}
		System.out.println("x = " + xy[0] + ", y = " + xy[4]);
		for (int y = 0; y < xy[4]; y++) {
			for (int x = 1; x <= xy[0]; x++) {
				int temp = data[x + y * 32];
				if (temp < 0) {
					temp = 256 + temp;
				}
				int k = 4194304 + ((x * 16) | (y * 1048576));
				if (temp != 143) {
					User32.INSTANCE.SendMessageA(hwnd, 513, 0, k);
					User32.INSTANCE.SendMessageA(hwnd, 514, 0, k);
				}
			}
		}
		Kernel32.INSTANCE.CloseHandle(hProcess);
		return true;
	}

	public static void main(String[] args) {
		System.out.println(Saolei.execute());

	}
}
